<?php
/**
 * Description of Account
 *
 * @author Laszlo.Pokoradi
 */
class Account extends Model {
  
  public function __construct() {
    parent::Model();
  }

  public function create($data) {
    foreach ($data as $key => $value) {
      $this->db->set($key, $value);
    }
    $query = $this->db->insert(ACCOUNT_TABLENAME);

    if(!$query) {
      log_message('error','Error during account creation! SQL: '.$this->db->insert_string());
      return FALSE;
    } else {
      $id = $this->db->insert_id();
      log_message('info','Account ('.$id.') was inserted to the database!');
      return $id;
    }
  }

  public function read($id) {
    $this->db->where('id',$id);
    $query = $this->db->insert(ACCOUNT_TABLENAME);

    if(!$query || $query->num_rows() != 1) {
      log_message('error','Error during account read! SQL: '.$this->db->last_query());
      return FALSE;
    } else {
      log_message('info','Account ('.$id.') was read from the database!');
      return $query->row_array();
    }
  }

  public function update($id, $data) {
    $old = $this->read($id);
    $histories = array();
    foreach ($data as $key => $value) {
      if ($old[$key] != $value) {
        $colHist = array('columnName' => $key,'oldValue' => $old[$key], 'newValue' => $value);
        $this->db->set($key, $value);
        array_push($histories, $colHist);
      }
    }
    $this->db->where('id',$id);
    $query = $this->db->update(ACCOUNT_TABLENAME);

    if(!$query || $this->db->affected_rows() != 1) {
      log_message('error','Error during account update! SQL: '.$this->db->update_string());
      return FALSE;
    } else {
      foreach ($histories as $history) {
        $this->db->set('table',ACCOUNT_TABLENAME);
        $this->db->set('recordId',$id);
        $this->db->set('columnName',$history['columnName']);
        $this->db->set('oldValue',$history['oldValue']);
        $this->db->set('newValue',$history['newValue']);
      }

      log_message('info','Account ('.$id.') was updated in the database!');
      return TRUE;
    }
  }

  public function delete($id) {
    $this->db->where('id', $id);
    $query = $this->db->delete(ACCOUNT_TABLENAME);

    if(!$query || $this->db->affected_rows() != 0) {
      log_message('error','Error during account delete! SQL: '.$this->db->last_query());
      return FALSE;
    } else {
      log_message('info','Account ('.$id.') was deleted from the database!');
      return TRUE;
    }
  }

//  public function loadFromFile($fileName = FILEPATH_ACCOUNT, $unlink = FALSE) {
//    $this->load->helper('file');
//    $doc = new DOMDocument('1.0', 'utf-8');
//
//    $tAccountFile = read_file($fileName);
//    if (!$tAccountFile) {
//      log_message('error','Unable to open account file: '.$fileName);
//      return FALSE;
//    }
//
//    $tDocument = $doc->loadXML($tAccountFile);
//
//    if (!$tDocument) {
//      log_message('error','The account file is not valid XML!');
//      return FALSE;
//    } else {
//      $xPath = new DOMXPath($doc);
//
//      $qResult = $xPath->query($this->USERNAME_PATH);
//      $data[ACCOUNT_COL_USERNAME] = $qResult->item(0)->nodeValue;
//
//      $qResult = $xPath->query($this->NATIONALITY_PATH);
//      $data[ACCOUNT_COL_NATIONALITY] = $qResult->item(0)->nodeValue;
//
//      $qResult = $xPath->query($this->CLUBID_PATH);
//      $data[ACCOUNT_COL_CLUBID] = $qResult->item(0)->nodeValue;
//
//      $qResult = $xPath->query($this->CLUBNAME_PATH);
//      $data[ACCOUNT_COL_CLUBNAME] = $qResult->item(0)->nodeValue;
//
//      $qResult = $xPath->query($this->ISVIP_PATH);
//      $data[ACCOUNT_COL_VIP] = (boolean) $qResult->item(0)->nodeValue;
//    }
//
//    if ($unlink) {
//      unlink($fileName);
//    }
//    return $data;
//  }
//
//  public function downloadOwnAccountWith($userName, $password) {
//    $this->load->helper('file');
//    $this->load->helper('curl');
//    $this->load->helper('crypt');
//
//    $accountURL = 'http://www.freekick.org/public/getdata.php?getdata=account'
//        .'&datatype=xml&username='.$userName.'&password='.decrypt($password);
//
//    $accountFile = read_url($accountURL);
//
//    if (!$accountFile) {
//      log_message('error','Unable to open account URL!');
//      return false;
//    }
//
//    $tmpfname = tempnam(DIRPATH_TEMP, "acc");
//
//    $accountXMLFile = write_file($tmpfname,$accountFile);
//    if (!$accountXMLFile) {
//      log_message('error','Unable to open account file!');
//      return false;
//    }
//
//    log_message('info','Account xml created!');
//    return $tmpfname;
//  }
//
//  public function downloadOwnAccount() {
//    $userName = $this->session->userdata('username');
//    $password = $this->session->userdata('password');
//
//    return $this->downloadOwnAccountWith($userName,$password);
//  }
}
?>
